window.onload = function () {
    if (localStorage.length < 1)    //本地浏览器是否存在问卷数据
        location.assign("/nullAdd.html")
    for (j = 1; j <= localStorage.length; j++) {    //循环添加问卷数据
        var d = new Date();//当前日期
        var nowD = d.getFullYear().toString() //当前日期的年
        if (d.getMonth() + 1 < 10)//当前日期的月 小于10时在前面添加一个0
            nowD = nowD + "-0" + (d.getMonth() + 1).toString()
        else
            nowD = nowD + "-" + (d.getMonth() + 1).toString()
        if (d.getDate() < 10)//当前日期的日 小于10时在前面添加一个0
            nowD = nowD + "-0" + d.getDate().toString()
        else
            nowD = nowD + "-0" + d.getDate().toString()
        var index = localStorage.key(j - 1)
        if (isNaN(index))
            continue
        var reuslt = JSON.parse(localStorage.getItem(index))//从本地浏览器储存中获取问卷数据

        if (reuslt.time < nowD) {//通过时间判断是否已经截至即结束
            reuslt.state = "已结束"
            localStorage.removeItem(index)//将问卷数据存入本地浏览器
            localStorage.setItem(index, JSON.stringify(reuslt))//将问卷数据存入本地浏览器
        }
        var str = ""
        str += '<tr id="tr' + (index) + '">'
        str += '    <td><input class="check" type="checkbox" value="' + (index) + '"></td>'
        str += '    <td>' + (reuslt.title) + '</td>'
        str += '    <td>' + (reuslt.time) + '</td>'
        str += '    <td id="state' + (index) + '">' + (reuslt.state) + '</td>'
        if (reuslt.state == "未发布") {//未发布时可进行的操作
            str += '    <td>'
            str += '        <button type="button" class="do" onclick="edit(' + (index) + ')">编辑</button>'
            str += '        <button type="button" class="do" onclick="dele(' + (index) + ')">删除</button>'
            str += '    </td>'
            str += '</tr>'
            var content = document.querySelector("tbody").innerHTML
            document.querySelector("tbody").innerHTML = str + content
        } else if (reuslt.state == "发布中") {//发布中时可进行的操作
            str += '    <td>'
            str += '        <button type="button" class="do" onclick="dele(' + (index) + ')">删除</button>'
            str += '        <button type="button" class="do" onclick="fillIn(' + (index) + ')">填写问卷</button>'
            str += '        <button type="button" class="do" onclick="getDetial(' + (index) + ')">查看数据</button>'
            str += '    </td>'
            str += '</tr>'
            var content = document.querySelector("tbody").innerHTML
            document.querySelector("tbody").innerHTML = str + content
            var id = "state" + index.toString()
            var state = document.getElementById(id);
            state.style.color = "lightgreen"
        } else {//已结束时可进行的操作
            str += '    <td>'
            str += '        <button type="button" class="do" onclick="dele(' + (index) + ')">删除</button>'
            str += '        <button type="button" class="do" onclick="getDetial(' + (index) + ')">查看数据</button>'
            str += '    </td>'
            str += '</tr>'
            var content = document.querySelector("tbody").innerHTML
            document.querySelector("tbody").innerHTML = str + content
        }
    }
}
function edit(index) {//编辑问卷
    window.location.href = '/add.html?number=' + index
}
function dele(index) {//删除问卷
    var id = "#tr" + index;
    var d = confirm("是否要删除");
    if (d) {
        document.querySelector(id).remove();//移除问卷标签
        localStorage.removeItem(index)//移除本地浏览器中对应的问卷数据
        alert("删除成功")
    }
    if (localStorage.length == 1) {//问卷数据为空时跳转到另一个页面
        localStorage.removeItem("number")//移除问卷索引
        location.assign("/nullAdd.html")
    }
}
function fillIn(index) {//填写问卷
    window.location.href = '/fillIn.html?number=' + index

}
function getDetial(index) {//查看问卷数据
    window.location.href = '/data.html?number=' + index
}
function selectAll(checkbox) {//全选/全不选
    var checked = checkbox.checked;
    var checkboxes = document.querySelectorAll("input");
    for (var i in checkboxes) {
        checkboxes[i].checked = checked ? "checked" : "";
    }
}
function deletc() {//批量删除
    var d = confirm("是否要删除");
    if (d) {
        var checkboxes = document.querySelectorAll(".check");
        for (i = 0; i < checkboxes.length; i++) {
            if (checkboxes[i].checked) {
                var ind = checkboxes[i].value
                var id = "#tr" + ind.toString()
                document.querySelector(id).remove();//移除问卷标签
                localStorage.removeItem(ind)//移除本地浏览器中对应的问卷数据
            }
        }
    }
    if (localStorage.length == 1) {//问卷数据为空时跳转到另一个页面
        localStorage.removeItem("number")//移除问卷索引
        location.assign("/nullAdd.html")
    }
}